package com.app.web.user.myCenter;

import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.DateUtil;
import com.app.utils.base.j2se.Logger;
import com.app.utils.base.j2se.StringHandler;
import com.app.utils.spring.properties.Config;
import com.app.web.expectMoney.ExpectMoneyService;
import com.app.web.order.OrderService;
import com.app.web.user.info.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tbyy-app->MyCenter
 * @Auther: 王伟
 * @Date: 2019/12/13 10:57
 * @Description: 个人中心-我的
 */
@RestController
@RequestMapping("/my/")
public class MyCenterController {
    @Autowired
    private MyCenterService myCenterService;
    @Autowired
    private ExpectMoneyService expectMoneyService;

    /**
     * 我的订单数据及预估金额
     * @param uid
     * @return
     */
    @RequestMapping("getMyOrderMoney")
    //    @RedisCache(timeOut = 5)
    public String getMyOrderMoney(@RequestAttribute long uid){
        //当天预估收益
        int[] day = DateUtil.dateInfo(1, 0);
        int beginTime = day[2];
        int endTime = day[2];
        float dayExpectMoney = expectMoneyService.getDateExpectMoney(uid,beginTime,endTime);
        //本月预估收益
        int[] lastMonth = DateUtil.dateInfo(3, 0);
        int lastBeginTime = lastMonth[0];
        int lastendTime = lastMonth[1];
        float lastMonthMxpectMoney = expectMoneyService.getDateExpectMoney(uid,lastBeginTime,lastendTime);
        //提现文案提示
        String withdrawal_txt = Config.pp.get("user.withdrawal.txt");
        Map<String,Object> obj = new HashMap<>();
        obj.put("dayExpectMoney",dayExpectMoney);
        obj.put("lastMonthMxpectMoney",lastMonthMxpectMoney);
        obj.put("withdrawalTxt",withdrawal_txt);
        return ResultUtil.success(obj);
    }


    /**
     * 收获地址
     *
     * @param uid
     * @param name
     * @param phone
     * @param address
     * @param defaultStatus
     * @return
     */
    @RequestMapping("address/save")
    public String addressSave(@RequestAttribute long uid,@RequestParam(defaultValue = "0")int id, String name,
                              String phone, String province,String city,String county,String address,String addrInfo,
                              @RequestParam(defaultValue = "0") String defaultStatus) {
        Map<String, Object> param = new HashMap<>();
        if(StringHandler.isEmpty(address)){
            address = province+city+county;
        }
        param.put("uid", uid);
        param.put("id", id);
        param.put("name", name);
        param.put("phone", phone);
        param.put("address", address);
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        param.put("addrInfo", addrInfo);
        param.put("defaultStatus", defaultStatus);
        try {
            myCenterService.saveUserAddress(param);
            return ResultUtil.success("添加完成");
        } catch (Exception e) {
            Logger.error("地址添加失败",e);
            return ResultUtil.error(6001,"添加地址失败");
        }
    }

    @RequestMapping("address/list")
    public String addressList(@RequestAttribute long uid, @RequestParam(defaultValue = "0") int defaultStatus) {
        try {
            List<Map<String, Object>> mapList = myCenterService.selectUserAddress(uid, defaultStatus);
            return ResultUtil.success(mapList);
        } catch (Exception e) {
            Logger.error("用户查询地址失败",e);
            return ResultUtil.error(6001,"查询失败");
        }
    }

    @RequestMapping("address/default")
    public String addressSetDefault(@RequestAttribute long uid, int id, @RequestParam(defaultValue = "0") int defaultStatus) {
        try {
            //重置默认地址
            myCenterService.updateUserAddress(0, uid, 0);
            //添加默认地址
            int i = myCenterService.updateUserAddress(id, 0, defaultStatus);
            if (i != 1) return ResultUtil.error(6001,"重置默认地址失败");
            return ResultUtil.success();
        } catch (Exception e) {
            Logger.error("用户设置默认地址失败",e);
            return ResultUtil.error(6001,"用户设置默认地址失败");
        }
    }

    @RequestMapping("address/delete")
    public String addressDelete(@RequestAttribute long uid,int id) {
        try {
            int i = myCenterService.deleteUserAddress(uid,id);
            if (i != 1) return ResultUtil.error(6001,"删除失败");
            return ResultUtil.success();
        } catch (Exception e) {
            Logger.error("地址删除失败",e);
            return ResultUtil.error(6001,"地址删除失败");
        }
    }
}
